package org.example.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.entity.*;
import org.example.mapper.EmpExprMapper;
import org.example.mapper.EmpMapper;
import org.example.service.EmpService;
import org.example.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageBean empPage(EmpQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<Emp> list = empMapper.list(param);
        Page<Emp> page1 = (Page<Emp>)list;
        PageBean pageBean = new PageBean(page1.getTotal(),page1.getResult());
        return pageBean;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        //补全信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        List<EmpExpr> exprList = emp.getExprList();
        //补充emp中expList中每一个expr对象的Id
        //保存员工的工作经历
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }

    }
    @Transactional
    @Override
    public void deleteEmpsByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByIds(ids);
    }

    @Override
    public Emp selectEmpMsgById(Integer id) {
     Emp emp = empMapper.selectById(id);
        return emp;
    }
@Transactional
    @Override
    public void updateEmpMsg(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //删除员工旧的工作经历
    empExprMapper.deleteByIds(Arrays.asList(emp.getId()));
//        增加新的工作经历
    Integer id = emp.getId();
    List<EmpExpr> exprList = emp.getExprList();
    if (!CollectionUtils.isEmpty(exprList)){
        empExprMapper.insertBatch(exprList);
    }
    }

    @Override
    public String login(String username, String password) {
//        查看数据中是否有username对应数据，有的话封装成对应的Emp对象
        Emp emp = empMapper.selectByUsername(username,password);
//        emp不为空的话生成对应的token
        if (emp != null) {
            HashMap<String, Object> tokenMap = new HashMap<>();
            tokenMap.put("id",emp.getId());
            tokenMap.put("username",emp.getUsername());
            String token = JWTUtils.generateJwt(tokenMap);
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setId(emp.getId());
            loginInfo.setName(emp.getName());
            loginInfo.setToken(token);
            loginInfo.setUsername(emp.getUsername());
            return token;
        }
        return null;
    }
}
